//
//  DetailsOfMeetingFromTableViewCell.m
//  Gxj
//
//  Created by 刘伟 on 2017/9/18.
//  Copyright © 2017年 wkq. All rights reserved.
//

#import "DetailsOfMeetingFromTableViewCell.h"
#import "UILabel+ChangeLineSpaceAndWordSpace.h"

@interface DetailsOfMeetingFromTableViewCell ()
{
    BOOL _isSelect;
    CGSize _size;
}

@end

@implementation DetailsOfMeetingFromTableViewCell

// 重写表格位置，使每一行cell都分隔开
//- (void)setFrame:(CGRect)frame{
//    frame.origin.y += 0;
//    frame.size.height -= 5;
//    [super setFrame:frame];
//}

+ (CGFloat)cellDefaultHeight:(CommentariesFromModel *)entity
{
    //默认cell高度
    return kHeightScale(148);
}

+ (CGFloat)cellMoreHeight:(CommentariesFromModel *)entity
{
    //展开后得高度(计算出文本内容的高度+固定控件的高度)
    NSDictionary *attribute = @{NSFontAttributeName: [UIFont systemFontOfSize:16]};
    NSStringDrawingOptions option = (NSStringDrawingOptions)(NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading);
    CGSize size = [entity.textContent boundingRectWithSize:CGSizeMake(UIScreenWidth - 30, 100000) options:option attributes:attribute context:nil].size;;
    return size.height + 30;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        [self.shadowView addSubview:self.userHead];  // 用户头像
        [self.contentView addSubview:self.userName];// 用户名称
        [self.contentView addSubview:self.medal];  // 等级勋章
        [self.contentView addSubview:self.gradeLab];// 等级
        [self.contentView addSubview:self.contentCommentaries];// 评论内容
        [self.contentView addSubview:self.toViewMore];// 查看更多
        [self.contentView addSubview:self.thumpUp];  // 点赞图标
        [self.contentView addSubview:self.agreeNum];// 点赞数量
        [self.contentView addSubview:self.commenttariesNum];// 评论数
        [self.contentView addSubview:self.timeAgo];        // 评论时间
        [self.contentView addSubview:self.timeImage];     // 时间图片
        [self.contentView addSubview:self.shadowView];
        [self.contentView addSubview:self.showAllBtn];
        [self.contentView addSubview:self.bottomBgColor];
        [self showDetailOfMeetingCell];
    }
    return self;
}

//- (void)setModel:(CommentariesFromModel *)model
//{
//    _contentCommentaries.text = model.contentText;
//    
//    [self setupAutoHeightWithBottomView:_timeImage bottomMargin:10];
//}

- (void)showDetailOfMeetingCell
{
    _shadowView.sd_layout
    .topSpaceToView(self.contentView,kHeightScale(10))
    .leftSpaceToView(self.contentView,kWidthScale(15))
    .widthIs(kWidthScale(25))
    .heightIs(kHeightScale(25));
    
    _userHead.sd_layout
    .topSpaceToView(self.shadowView,kHeightScale(2))
    .leftSpaceToView(self.shadowView,kWidthScale(2))
    .rightSpaceToView(self.shadowView,kWidthScale(2))
    .bottomSpaceToView(self.shadowView,kHeightScale(2));
    
    _userName.sd_layout
    .topEqualToView(self.shadowView)
    .leftSpaceToView(self.shadowView,kWidthScale(5))
    .autoHeightRatio(0);
    [_userName setSingleLineAutoResizeWithMaxWidth:kWidthScale(150)];
    
    _medal.sd_layout
    .topSpaceToView(self.userName,kHeightScale(3))
    .leftEqualToView(self.userName)
    .widthIs(kWidthScale(10))
    .heightIs(kHeightScale(10));
    
    _gradeLab.sd_layout
    .centerYEqualToView(self.medal)
    .leftSpaceToView(self.medal,kWidthScale(3))
    .autoHeightRatio(0);
    [_gradeLab setSingleLineAutoResizeWithMaxWidth:kWidthScale(40)];
    
    
    
    _timeImage.sd_layout
    .bottomSpaceToView(self.contentView,kHeightScale(15))
    .leftEqualToView(self.contentCommentaries)
    .widthIs(kWidthScale(11))
    .heightIs(kHeightScale(11));
    
    _timeAgo.sd_layout
    .centerYEqualToView(self.timeImage)
    .leftSpaceToView(self.timeImage,kWidthScale(5))
    .autoHeightRatio(0);
    [_timeAgo setSingleLineAutoResizeWithMaxWidth:kWidthScale(80)];
    
    _agreeNum.sd_layout
    .centerYEqualToView(self.timeImage)
    .rightSpaceToView(self.contentView,kWidthScale(15))
    .autoHeightRatio(0);
    [_agreeNum setSingleLineAutoResizeWithMaxWidth:kWidthScale(60)];
    
    _thumpUp.sd_layout
    .centerYEqualToView(self.timeImage)
    .rightSpaceToView(self.agreeNum,kWidthScale(5))
    .widthIs(kWidthScale(11))
    .heightIs(kHeightScale(11));
    
    _showAllBtn.sd_layout
    .centerYEqualToView(self.shadowView)
    .rightSpaceToView(self.contentView,kWidthScale(10))
    .widthIs(kWidthScale(40))
    .heightIs(kHeightScale(20));
    
    _bottomBgColor.sd_layout
    .bottomSpaceToView(self.contentView,kHeightScale(5))
    .leftEqualToView(self.contentView)
    .rightEqualToView(self.contentView)
    .heightIs(kHeightScale(5));
    
    if (!_isSelect) {
//        _contentCommentaries.sd_layout
//        .topSpaceToView(self.shadowView,kHeightScale(10))
//        .bottomSpaceToView(self.timeImage,kHeightScale(10))
//        .leftEqualToView(self.shadowView)
//        .rightSpaceToView(self.contentView,kWidthScale(15))
//        .heightIs(40);
        _contentCommentaries.frame = CGRectMake(kWidthScale(15), kHeightScale(45), UIScreenWidth - kWidthScale(30), kHeightScale(70));
    }else{
//        _contentCommentaries.sd_layout
//        .topSpaceToView(self.shadowView,kHeightScale(10))
//        .bottomSpaceToView(self.timeImage,kHeightScale(10))
//        .leftEqualToView(self.shadowView)
//        .rightSpaceToView(self.contentView,kWidthScale(15))
//        .heightIs(_size.height);
        _contentCommentaries.frame = CGRectMake(kWidthScale(15), kHeightScale(45), UIScreenWidth - kWidthScale(30), _size.height - 50);
    }
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

// 用户头像
- (UIButton *)userHead
{
    if (!_userHead)
    {
        _userHead = [UIButton buttonWithType:UIButtonTypeCustom];
        [_userHead setBackgroundImage:[UIImage imageNamed:@"toixiang"] forState:0];
    }
    return _userHead;
}
// 用户名称
- (UILabel *)userName
{
    if (!_userName)
    {
        _userName = [[UILabel alloc] init];
        _userName.text = @"每天都蹲点";
        _userName.font = [UIFont systemFontOfSize:kWidthScale(12)];
        _userName.textColor = [UIColor colorWithHexString:@"#333333"];
    }
    return _userName;
}
// 等级勋章
- (UIImageView *)medal
{
    if (!_medal)
    {
        _medal = [[UIImageView alloc] init];
        _medal.image = [UIImage imageNamed:@"dengjis"];
    }
    return _medal;
}
// 等级
- (UILabel *)gradeLab
{
    if (!_gradeLab)
    {
        _gradeLab = [[UILabel alloc] init];
        _gradeLab.text = @"10级";
        _gradeLab.textColor=[UIColor colorWithHexString:@"#999999"];
        _gradeLab.font=[UIFont systemFontOfSize:kWidthScale(11)];
        [_gradeLab updateLayout];
    }
    return _gradeLab;
}
// 评论内容
- (UILabel *)contentCommentaries
{
    if (!_contentCommentaries)
    {
        _contentCommentaries = [[UILabel alloc] init];
        _contentCommentaries.text = @"读大学，哪怕不是一本二本，也可以让你在面对残酷的社会之前有个缓冲，不至于被虐的太惨。大学相当于进社会之前的一个演习，如果你没有经历过大学生活的摧残，那你的人生将是不完整的。步入社会的你还是懵懂无知的，还是天真烂漫的，但是……";
        _contentCommentaries.textColor = [UIColor colorWithHexString:@"#666666"];
        _contentCommentaries.numberOfLines = 0;
        _contentCommentaries.font = [UIFont systemFontOfSize:kWidthScale(12)];
//        [_contentCommentaries alignmentRectInsets];
    }
    return _contentCommentaries;
}
// 查看更多
- (UIButton *)toViewMore
{
    if (!_toViewMore)
    {
        _toViewMore = [UIButton buttonWithType:UIButtonTypeCustom];
    }
    return _toViewMore;
}
// 点赞图标
- (UIButton *)thumpUp
{
    if (!_thumpUp)
    {
        _thumpUp = [UIButton buttonWithType:UIButtonTypeCustom];
        [_thumpUp setImage:[UIImage imageNamed:@"zans"] forState:0];
    }
    return _thumpUp;
}
// 赞同
- (UILabel *)agreeNum
{
    if (!_agreeNum)
    {
        _agreeNum = [[UILabel alloc] init];
        _agreeNum.text = @"89";
        _agreeNum.textColor = [UIColor colorWithHexString:@"#999999"];
        _agreeNum.font = [UIFont systemFontOfSize:kWidthScale(12)];
        [_agreeNum updateLayout];
    }
    return _agreeNum;
}
// 评论数
- (UILabel *)commenttariesNum
{
    if (!_commenttariesNum)
    {
        _commenttariesNum = [[UILabel alloc] init];
    }
    return _commenttariesNum;
}
// 评论图标
- (UIImageView *)timeImage
{
    if (!_timeImage)
    {
        _timeImage = [[UIImageView alloc] init];
        _timeImage.image = [UIImage imageNamed:@"cxshijian"];
    }
    return _timeImage;
}
// 评论时间
- (UILabel *)timeAgo
{
    if (!_timeAgo)
    {
        _timeAgo = [[UILabel alloc] init];
        _timeAgo.text = @"08:15";
        _timeAgo.textColor = [UIColor colorWithHexString:@"#999999"];
        _timeAgo.font = [UIFont systemFontOfSize:kWidthScale(12)];
        [_timeAgo updateLayout];
    }
    return _timeAgo;
}

- (UIView *)shadowView
{
    if (!_shadowView)
    {
        _shadowView = [[UIView alloc] init];
        _shadowView.backgroundColor = [UIColor colorWithRed:(213.0/255) green:(213.0/255) blue:(213.0/255) alpha:1];;
        _shadowView.layer.masksToBounds=YES;
        _shadowView.layer.cornerRadius= kWidthScale(12.5);
        _shadowView.layer.shadowColor = [UIColor blackColor].CGColor;
        _shadowView.layer.shadowOffset = CGSizeMake(-3, 0);
        _shadowView.layer.shadowOpacity = 0.4;
        _shadowView.layer.shadowRadius = 2.0;
        _shadowView.clipsToBounds = NO;
    }
    return _shadowView;
}

- (UILabel *)bottomBgColor
{
    if (!_bottomBgColor)
    {
        _bottomBgColor = [[UILabel alloc] init];
        _bottomBgColor.backgroundColor = [UIColor colorWithRed:242.0/255.0 green:242.0/255.0 blue:242.0/255.0 alpha:1];
    }
    return _bottomBgColor;
}

- (UIButton *)showAllBtn
{
    if (!_showAllBtn)
    {
        _showAllBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_showAllBtn setTitle:@"展开" forState:0];
        _showAllBtn.titleLabel.font = [UIFont systemFontOfSize:kWidthScale(12)];
        [_showAllBtn setTitleColor:[UIColor colorWithRed:22.0/255.0 green:129.0/255.0 blue:249.0/255.0 alpha:1] forState:0];
        
        [_showAllBtn addTarget:self action:@selector(showMoreText) forControlEvents:UIControlEventTouchUpInside];
    }
    return _showAllBtn;
}

- (void)showMoreText
{
    //将当前对象的isShowMoreText属性设为相反值
    self.model.isShowMoreText = !self.model.isShowMoreText;
    if (self.showMoreTextBlock)
    {
        self.showMoreTextBlock(self);
    }
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    _contentCommentaries.text = self.model.textContent;
    _userName.text = self.model.textName;
    [_userName updateLayout];
    [UILabel changeLineSpaceForLabel:_contentCommentaries WithSpace:kHeightScale(3)];
    if (self.model.isShowMoreText)
    {
        _isSelect = self.model.isShowMoreText;
        ///计算文本高度
        NSDictionary *attribute = @{NSFontAttributeName: [UIFont systemFontOfSize:16]};
        NSStringDrawingOptions option = (NSStringDrawingOptions)(NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading);
        CGSize size = [self.model.textContent boundingRectWithSize:CGSizeMake(UIScreenWidth - 30, 100000) options:option attributes:attribute context:nil].size;
//        [_contentCommentaries setFrame:CGRectMake(15, 30, UIScreenWidth - 30, size.height)];
        _size = size;
        [_showAllBtn setTitle:@"收起" forState:UIControlStateNormal];
        _contentCommentaries.frame = CGRectMake(kWidthScale(15), kHeightScale(45), UIScreenWidth - kWidthScale(30), _size.height - 50);
//        [_contentCommentaries alignmentRectInsets];
//        [self showDetailOfMeetingCell];
//        _showAllBtn.hidden = YES;
    }
    else
    {
        [_showAllBtn setTitle:@"展开" forState:UIControlStateNormal];
//        [_contentCommentaries setFrame:CGRectMake(15, 30, UIScreenWidth - 30, 35)];
        _contentCommentaries.frame = CGRectMake(kWidthScale(15), kHeightScale(45), UIScreenWidth - kWidthScale(30), kHeightScale(70));
//        [self showDetailOfMeetingCell];
    }
    
}

@end
